<?php if(!defined('BASEPATH')) exit('No direct access to this file allowed');

/******************************************************************************\
 *  ErrorHandler
 *
 *  General purpose error handling function. This class centralizes all error
 *  reporting functions (even PHP exceptions) and handles them with the
 *  preferred mechanism specified by the user.
 *
 *  Because this is generally the first class instantiated after IgnitionBase
 *  and ComponentFactory - before and Logging mechanism is loaded, any
 *  pre-init errors are output directly to the screen. Post-init errors
 *  are sent to the screen using a template from a file.
 *
 *  Error does not use the Render component to render its error screens for
 *  good reasons. The render component is usually loaded AFTER the error
 *  component is loaded, and is only an auxiliary component, the error
 *  component should NOT depend on the render component to output its
 *  error messages.
 *
 *  Change Log
 *  0.1     -   Initial version of Error
 *
 *  Standard Tags:
 *  @author     Ruben Tan
 *  @copyright  Copyright (c) 2008 Rendervault
 *
 *  Developer Tags:
 *  @package    system
 *  @subpackage core
\******************************************************************************/
class ErrorHandler
{    
    protected $IB = null;
    
    const GENERAL = 'Error General';
    const ERROR404 = 'Error 404';
    const EXCEPTION = 'Error Exception';
    const GRACEFUL = 'Error Graceful';

    /**--------------------------------------------------------------------------------------
     *  Error constructor
     *
     *  @author     Ruben Tan
     *  
     *  @access     protected
     *  @param      type        description
     *  @return     type        description
     */
    public function __construct( $IB )
    { 
        $this->IB = $IB;
        
        set_error_handler( array( $this, 'error' ) );
        set_exception_handler( array( $this, 'exception' ) );
    }
    
    /**--------------------------------------------------------------------------------------
     *  Handles error message
     *
     *  @author     Ruben Tan
     *  
     *  @access     public
     *  @param      type        description
     *  @return     type        description
     */
    public function error( $severity, $message, $filepath, $line )
    {
        $this->_out( ErrorHandler::GENERAL, $message, $filepath, $line );
    }
    
    /**--------------------------------------------------------------------------------------
     *  Handles exception
     *
     *  @author     Ruben Tan
     *  
     *  @access     public
     *  @param      type        description
     *  @return     type        description
     */
    public function exception( $e )
    {
        $this->_out( ErrorHandler::EXCEPTION, $e->getMessage(), $e->getFile(), $e->getLine() );
    }
    
    /**--------------------------------------------------------------------------------------
     *  outputs
     *
     *  @author     Ruben Tan
     *  
     *  @access     protected
     *  @param      type        description
     *  @return     type        description
     */
    protected function _out( $type, $message, $filepath, $line )
    {
        $meta = array(
                'type' => $type,
                'message' => $message,
                'line' => $line,
                'filepath' => $filepath,
                'profile_data' => (isset($this->profiler)) ? $this->profiler->get_profile_data() : NULL
                );

        $this->_out_render( $meta );
    }
    
    /**--------------------------------------------------------------------------------------
     *  Renders data to the standard output subsystem
     *
     *  @author     Ruben Tan
     *  
     *  @access     protected
     *  @param      type        description
     *  @return     type        description
     */
    protected function _out_render( $meta )
    {
        $path = SYSPATH . 'resources' . DS . 'error' . DS . str_replace( ' ', '', $meta['type'] ) . EXT;

        $this->IB->render->flush_view( $path, $meta );
        
        die();
    }
}